package edu.scau.mis.rest.service.impl;

import edu.scau.mis.core.redis.RedisCache;
import edu.scau.mis.rest.entity.RestOrder;
import edu.scau.mis.rest.entity.RestSeat;
import edu.scau.mis.rest.entity.RestUser;
import edu.scau.mis.rest.mapper.RestOrderMapper;
import edu.scau.mis.rest.mapper.RestSeatMapper;
import edu.scau.mis.rest.mapper.RestUserMapper;
import edu.scau.mis.rest.service.RestSeatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

/**
 * (RestSeat)表服务实现类
 *
 * @author makejava
 * @since 2022-03-11 15:20:51
 */
@Service("restSeatService")
public class RestSeatServiceImpl implements RestSeatService {
    @Resource
    private RestSeatMapper restSeatMapper;

    @Autowired
    RestOrderMapper restOrderMapper;

    @Autowired
    RestUserMapper restUserMapper;

    @Autowired
    RedisCache redisCache;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public RestSeat queryById(Long id) {
        return this.restSeatMapper.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param restSeat 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<RestSeat> queryByPage(RestSeat restSeat, PageRequest pageRequest) {
        long total = this.restSeatMapper.count(restSeat);
        return new PageImpl<>(this.restSeatMapper.queryAllByLimit(restSeat, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param restSeat 实例对象
     * @return 实例对象
     */
    @Override
    public RestSeat insert(RestSeat restSeat) {
        this.restSeatMapper.insert(restSeat);
        return restSeat;
    }

    /**
     * 修改数据
     *
     * @param restSeat 实例对象
     * @return 实例对象
     */
    @Override
    public RestSeat update(RestSeat restSeat) {
        this.restSeatMapper.update(restSeat);
        return this.queryById(restSeat.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.restSeatMapper.deleteById(id) > 0;
    }

    /**
     * 选座位
     *
     * @param openid
     * @param seatNo
     */
    @Override
    public void chooseSeat(String openid, String seatNo){
        if(redisCache.getListLength(openid) == 0){
            //标识用户与餐桌
            redisCache.addList(openid, UUID.randomUUID().toString());
            redisCache.addList(openid, seatNo);
            //初始化总价为0
            redisCache.addList(openid, BigDecimal.valueOf(0));
        }
    }

    /**
     * 通过餐位号查询餐位
     *
     * @param seatNo
     * @return
     */
    @Override
    public RestSeat querySeatBySeatNo(String seatNo){
        return this.restSeatMapper.querySeatBySeatNo(seatNo);
    }

    /**
     * 切换用户餐位号
     *
     * @param openid
     * @param seatNo
     */
    @Override
    public void changeSeat(String openid, String seatNo){
        redisCache.setListByIndex(openid, 1, seatNo);
    }

    /**
     * 改变餐位状态为占用中
     *
     * @param seatNo
     * @return
     */
    @Override
    public int updateSeatStatus(String seatNo, Integer isOccupied){
        return this.restSeatMapper.updateSeatStatus(seatNo, isOccupied);}

    /**
     * 获取所有未占用餐桌
     *
     * @return
     */
    @Override
    public List<RestSeat> queryAllUnoccupiedSeat(String openid){
        List<RestSeat> restSeatList = new LinkedList<>();

        //获取用户未完成的订单
        RestUser restUser = restUserMapper.queryRestUserByOpenid(openid);
        List<RestOrder> restOrderList = restOrderMapper.queryNowOrderByUserId(restUser.getId());

        //如果该用户存在未完成的订单，允许该用户在此餐位继续点餐，优先显示
        if(restOrderList.size() != 0){
            for(int i = 0; i < restOrderList.size(); i++){
                RestSeat restSeat = restSeatMapper.queryById(restOrderList.get(i).getSeatId());
                restSeatList.add(restSeat);
            }
        }

        //添加其余未占用餐位
        List<RestSeat> restSeats = restSeatMapper.queryAllUnoccupiedSeat();
        for(int i = 0; i < restSeats.size(); i++){
            restSeatList.add(restSeats.get(i));
        }

        return restSeatList;
    }

    /**
     * 判断座位号是否可用
     *
     * @param openid
     * @return
     */
    @Override
    public boolean isSeatCanUse(String openid){

        //获取用户未完成的订单
        RestUser restUser = restUserMapper.queryRestUserByOpenid(openid);
        List<RestOrder> restOrderList = restOrderMapper.queryNowOrderByUserId(restUser.getId());
        //用户选择的餐位号
        String seatNo = redisCache.getListByIndex(openid, 1).toString();


        boolean flag = true;//中间变量，默认为true
        //用户点餐时如果餐位占用中
        if(restSeatMapper.querySeatBySeatNo(seatNo).getIsOccupied() == 1){
            //不存在未完成订单，不予以点餐
            if(restOrderList.size() == 0) {
                return false;
            }else{
                //存在未完成订单，用户追加餐品时，如果与第一次点餐座位不符，则不予点餐
                flag = false;//中间变量，若存在与追加餐品的餐位号，则设置为true
                for(int i = 0; i < restOrderList.size(); i++){
                    if(restSeatMapper.querySeatBySeatNo(seatNo).getId() == restOrderList.get(i).getSeatId()){
                        flag = true;
                    }
                }
            }
        }

        return flag;
    }
}
